package com.shisfish.library.Prere;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import com.shisfish.library.Impl.MessyImpl;
import com.shisfish.library.Model.Messy;
import com.shisfish.library.Utils.CacheUtils;
import com.shisfish.library.Utils.Global.GlobalVariable;

public class GetSysMailbox1 extends GlobalVariable {

	public static String[] getSysMailbox(HttpServletRequest request) {
		
		//HttpServletRequest request = null;
		
		boolean flag = false;
		
		String[] str = new String[2];
		
		// 先从缓存中去
		Cache cache = CacheUtils.getCache(SYS_MAILBOX, request);
		if (cache != null) {
			
			Element e1 = cache.get("email");
			Element e2 = cache.get("pass");
			//Element e3 = cache.get("ran");
			
			if (e1 == null || e2 == null) {
				flag = true;
			}
			else{
				str[0] = e1.getObjectValue().toString();
				str[1] = e2.getObjectValue().toString();
				return str;
			}
			
		} else {
			flag = true;
		}
		
		if (flag) {

			// 从数据库中取
			List<Messy> messys = new MessyImpl().selectByFcode(1);
			
			if (messys.size() > 0) {
				str[0] = messys.get(0).getContent();
				str[1] = messys.get(1).getContent();
				
				// 把系统邮箱存到缓存中
				Cache cache1 = CacheUtils.getCache(SYS_MAILBOX, request);
				
				if (cache1 != null) {
					cache1.put(new Element("email", str[0]));
					cache1.put(new Element("pass", str[1]));
					cache1.put(new Element("ran", "ran"));
				}
				return str;
			}
		}
		return str;
	}
	
}
